<?php

/**
 * Msa_CampaignSmsObject - Gestion des campagnes SMS
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Msa_CampaignSmsObject extends Erra_Fields_Base_Object {

    function __construct($objectId, $buildArgs = null) {
        parent::__construct($objectId, $buildArgs);

        $title = Erra_Fields_Text::create('title')
                ->setInAdminList(true)
                ->setDatabaseType('VARCHAR', 256)
                ->setDescription('Titre de la campagne');

        $content_campaign = Erra_Fields_TextArea::create('content_campaign')
                ->setRows(3)
                ->setDatabaseType('TEXT')
                ->setDescription('Contenu de la campagne');

        $campaign_send = Erra_Fields_Composite::create('envoyer')
                ->setDatabaseType('TINYINT')
                ->setDescription('<div class="envoyer-composite">Envoyer</div>')
                ->setInAdminList(true);


        $location = Msa_LocationObject::getLocationArray();
        $location['-'] = 'Tous les lieux';
        $shop_target = Erra_Fields_ComboBox::create('shop_target')
                ->fill($location)
                ->setInAdminList(true)
                ->setDatabaseType('INT', 16)
                ->setDescription('Lieu de destination de la news');

        $date_envoi = Erra_Fields_Hidden::create('date_envoi')
                ->setDatabaseType('INT', 16);

        $this->addFields($shop_target, $title, $content_campaign, $campaign_send, $date_envoi);
        $this->setDescription('Campagne SMS');
        $this->setAddText('Cliquez ici pour créér une campagne SMS');
        $this->addAttribute('class', 'modal');
    }

    /**
     * postListInit() - Calcul du cout de la campagne et affichage du dernier envoi
     */
    public function postListInit() {
        $list = $this->getObjectDataArray();
        foreach ($list as $item) {
            $envoi = '';
            $query = Erra_Core_DatabasePdo::SELECT('SELECT date_envoi FROM msa_campaignsmsobject WHERE id=' . $item['id']);
            if (Erra_Core_DatabasePdo::rowCount()) {
                $query = $query->fetch();
                if (strlen($query->date_envoi) && is_numeric($query->date_envoi)) {
                    $heure = date('G', $query->date_envoi) . 'h' . date('i', $query->date_envoi);
                    $envoi = '<strong><font color="green">Dernier envoi : ' . date('d/m/Y', $query->date_envoi) . ' à ' . $heure . '</font></strong>';
                }
            }
            $item['envoyer'] = '<button data-attr-id="' . $item['id'] . '" class="send_masssms blue_button pointer" value="" name="" type="button" style="">Envoyer</button><div class="send_masssms_result">' . $envoi . '</div>';
            $item['envoyer'] .= '<br/><button data-attr-id="' . $item['id'] . '" class="count_masssms blue_button pointer" value="" name="" type="button" style="">Calculer le coût</button>';
        }
    }

}

?>